#pragma once


namespace xtb
{
	template<class A,class B>
	struct Bstreenode
	{
		Bstreenode* _left;
		Bstreenode* _right;
		A _key;
		B _val
		Bstreenode(const A& key,const B& val)
			:_left(nullptr)
			,_right(nullptr)
			,_key(key)
			,_val(val)
		{}
	};
	template<class A,class B>
	struct Bstree
	{
		typedef Bstreenode<A,B> node;
	public:
		Bstree(const Bstree<A,B>& s)
		{
			_root = Copy(s._root);
		}
		Bstree() = default;
		Bstree<A,B>& operator=(Bstree<A,B> t)
		{
			swap(_root, t._root);
			return *this;
		}
		bool insert(const A & key,const B&val)
			{
				if (_root == nullptr)
				{
					_root = new node(key);
					return true;
				}
				node* cur = _root;
				node* parent = nullptr;
				while (cur)
				{
					if (key > cur->_key)
					{
						parent = cur;
						cur = cur->_right
					}
					else if (key < cur->_key)
					{
						parend = cur;
						cur = cur->_left;
					}
					else
					{
						return false;
					}
				}
				cur = new node(key,val);
				if (key > parent->_key)
				{
					parent->_right = cur;
				}
				else
				{
					parent->_left = cur;
				}
				return true;
		 }
		bool find(const A& key)
		{
			node* cur = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					cur = cur->_right
				}
				else if (key < cur->_key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}
			return false;
		}
		bool Erase(const A&key)
		{
			if (_root == nullptr)
			{
				return false;
			}
			node* cur = _root;
			node* parent = nullptr;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right
				}
				else if (key < cur->_key)
				{
					parend = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->right
						}
						else
						{if (cur == parent->_right)
						{
							parent->_right = cur->_right;
						}
							else
						{
							parent->_left = cur->_right;
						}
					}
						delete cur;
						return true;
				}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left==cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
						return true;
					}
					else
						(
							node * remin = cur->_right;
					        node* reminparent = cur;
							while (remin->_left)
							{
								reminparent = remin;
								remin = remin->left;
							}
							cur->_key = remin->_key;
							if (remin == reminparent->_left)
								reminparent->_left = remin->_right;
							else
								reminparent->_right = remin->_right;
							delete rightMin;
							return true;
                         )
				}
			}
			return false;
	    }
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		~Bstree()
		{
			Destory(_root);
		}
		void _InOrder(node* root)
		{
			if (root == nullptr)
				return;

			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
		void Destory(node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			Destory(root->left);
			Destory(root->right);
			delete root;
		}
		node* Copy(node* root)
		{
			if (root == nullptr)
				return nullptr;

			node* newRoot = new node(root->_key);
			newRoot->_left = Copy(root->_left);
			newRoot->_right = Copy(root->_right);
			return newRoot;
		}
	private:
		node* _root=nullptr;
	};
}